﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArithmeticLibrary;
using GraphicLibrary;

namespace Nolineq
{
    /// <summary>
    /// Okno pro vykresleni funkce.
    /// </summary>
    public partial class DrawingForm : Form
    {
        #region Constructors

        public DrawingForm()
        {
            InitializeComponent();
        }

        public DrawingForm(string function, string start, string end)
        {
            InitializeComponent();

            tbFunction.Text = function;
            tbStart.Text = start;
            tbEnd.Text = end;
        }

        #endregion // Constructors

        private void panel_Paint(object sender, PaintEventArgs e)
        {
            if (tbFunction.Text == "")
                return;

            Expression exp;

            try
            {
                // zkusime parsnout vyraz a postavit z nej aritmeticky strom
                exp = new Expression(tbFunction.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Chybný výraz");
                return;
            }

            // pokud jsme uspeli a mame aritmeticky strom
            if (exp != null)
            {
                Function function = null;

                try
                {
                    // zkusime vytvorit funkci (definovat funkci, vypocitat jeji derivace)
                    function = new Function(exp);
                }
                catch (Exception)
                {
                    MessageBox.Show("Neplatná funkce.");
                    return;
                }

                double start = 0; double end = 0;
                try
                {
                    // zkusime nacist interval
                    start = double.Parse(tbStart.Text);
                    end = double.Parse(tbEnd.Text);

                    if (end <= start) throw new Exception();
                }
                catch (Exception)
                {
                    MessageBox.Show("Neplatný interval.");
                    return;
                }

                // pokud vse probehlo v poradku, vytvorime instanci objektu
                // pro vykresleni grafu funkce do panelu
                FunctionDrawer drawer = new FunctionDrawer(e.Graphics, panel, function, start, end);

                // vykreslime funkci
                drawer.DrawFunction();
            }
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            panel.Refresh();
        }

        private void DrawingForm_Resize(object sender, EventArgs e)
        {
            panel.Refresh();
        }

    }
}
